package fun.tan90.easy.binlog.manager.handler;

import com.mybatisflex.core.query.QueryWrapper;
import fun.tan90.easy.binlog.base.constants.EasyBinlogConstants;
import fun.tan90.easy.binlog.base.strategy.AbstractExecuteStrategy;
import fun.tan90.easy.binlog.manager.dataobject.enums.OutputStatusEnum;
import fun.tan90.easy.binlog.manager.dataobject.table.BinlogOutputTableDef;
import fun.tan90.easy.binlog.manager.dataobject.BinlogOutput;
import fun.tan90.easy.binlog.manager.dataobject.mapper.BinlogOutputMapper;
import lombok.extern.slf4j.Slf4j;
import org.noear.socketd.transport.core.Entity;
import org.noear.socketd.transport.core.entity.StringEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 19:42
 */
@Slf4j
@Component
public class GetOutputParamHandler implements AbstractExecuteStrategy<String, Entity> {

    @Resource
    BinlogOutputMapper binlogOutputMapper;

    @Override
    public String mark() {
        return EasyBinlogConstants.GET_OUTPUT_PARAM;
    }

    @Override
    public Entity executeResp(String outputCode) {
        BinlogOutput binlogOutput = binlogOutputMapper.selectOneByQuery(
                QueryWrapper.create()
                        .select(BinlogOutputTableDef.BINLOG_OUTPUT.OUTPUT_PARAMS, BinlogOutputTableDef.BINLOG_OUTPUT.OUTPUT_TYPE)
                        .where(BinlogOutputTableDef.BINLOG_OUTPUT.OUTPUT_CODE.eq(outputCode))
                        .and(BinlogOutputTableDef.BINLOG_OUTPUT.OUTPUT_STATUS.eq(OutputStatusEnum.可用.getKey()))
        );
        if (Objects.isNull(binlogOutput)) {
            return new StringEntity("");
        }
        String outputType = binlogOutput.getOutputType();
        String outputParams = binlogOutput.getOutputParams();
        return new StringEntity(outputParams).metaPut("output_type", outputType);
    }
}
